    # -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields, osv

class kderp_payment_term_code(osv.osv):
    _name='kderp.payment.term.code'
    _description = 'KDERP Payment Term Code'
    _order = 'code'
    _columns = {
                'code':fields.char('Code',size=3,required=True),
                'name':fields.char('Name',size=250,required=True),
                'type':fields.selection([('adv','Advance Payment'),('p','Progress'),('re','Retention'),('sch','Schedule'),('other','Others')],'Type',required=True),
                #'value':fields.float('Value (%)',required=True),
                'value': fields.selection([('percent', 'Percent')], 'Value',required=True),#('fixed', 'Fixed Amount')
                'value_amount':fields.float('Value (%)',required=True),
                'included_tax':fields.boolean('Included Tax'),
                'contract_term_line':fields.one2many('kderp.contract.payment.term.line','term_id','Contract Term Line')
                }
    _sql_constraints = [('payment_term_client_unique_name', "unique(code)",  'KDERP Error: The code number must be unique !')]

    def name_get(self, cr, uid, ids, context={}):
        if not len(ids):
            return []
        reads = self.read(cr, uid, ids, ['code','name'], context)
        res = []
        for record in reads:
            name = record['name']
            name = record['code']+' - '+name
            res.append((record['id'], name))
        return res
    
    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
        if not args:
            args=[]
        if not context:
            context={}
        ids = []
        if name:
            ids = self.search(cr, user, [('code','=',name)]+ args, limit=limit,context=context)
        if not ids:
            ids = self.search(cr, user, [('name','=',name)]+ args, limit=limit,context=context)
        if not ids:
            ids = self.search(cr, user, [('code','ilike',name)]+ args, limit=limit,context=context)
        if not ids:
            ids = self.search(cr, user, [('name','ilike',name)]+ args, limit=limit,context=context)

        return self.name_get(cr, user, ids, context=context)
    
    
    _defaults={
               'included_tax':lambda *a: True,
               'value': lambda *a: 'percent',
               'type':lambda *a: 'p',
               'value_amount':lambda *a: 0.0
               }
kderp_payment_term_code()